package com.sushuo.api.web;


import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.alibaba.fastjson.JSON;
import com.sushuo.api.web.error.ParamException;
import com.sushuo.base.util.filter.InterceptorUtil;


@ControllerAdvice
public class ExceptionTranslator {
    
    private static final Logger logger = LoggerFactory.getLogger(ExceptionTranslator.class);
    
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Result> processException(Exception ex, HttpServletRequest request)
        throws Exception {
        logger.error("", ex);
        logger.error("request url: {}", InterceptorUtil.getRequstAddress(request));
        logger.error("request content: {}", JSON.toJSON(request.getParameterMap()));
        return ResponseEntity.ok().body(new Result(StatusCode.ERROR));
    }
    
    @ExceptionHandler(ParamException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Result> processParamException(ParamException ex, HttpServletRequest request)
        throws Exception {
        logger.error("", ex);
        logger.error("errMsg: {}", ex.getErrorMessage());
        logger.error("request url: {}", InterceptorUtil.getRequstAddress(request));
        logger.error("request content: {}", JSON.toJSON(request.getParameterMap()));
        return ResponseEntity.ok().body(new Result(ex.getErrorCode(), ex.getErrorMessage()));
    }
    
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Result> processIllegalArgumentException(IllegalArgumentException ex, HttpServletRequest request)
        throws Exception {
        logger.error("", ex);
        logger.error("request url: {}", InterceptorUtil.getRequstAddress(request));
        logger.error("request content: {}", JSON.toJSON(request.getParameterMap()));
        return ResponseEntity.ok().body(new Result(StatusCode.PARAMTER.getValue(),ex.getMessage()));
    }
    
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Result> processBindException(HttpServletRequest request, BindException ex) {
        logger.error("", ex);
        logger.error("request url: {}", InterceptorUtil.getRequstAddress(request));
        logger.error("request content: {}", JSON.toJSON(request.getParameterMap()));
        String messageString = ex.getBindingResult().getFieldError().getDefaultMessage();
        // @NotEmpty 等拼写检查注解不用写上字段名了，直接写要做的规则
        // @NotEmpty(message="不能为空")
        // 而不用写成
        // @NotEmpty(message="XXX不能为空")
        messageString = ex.getBindingResult().getFieldError().getField().toString()+" "+messageString;
        return ResponseEntity.ok().body(new Result(StatusCode.PARAMTER.getValue(),messageString));
    }
    
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Result> processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        return ResponseEntity.ok().body(new Result(StatusCode.PARAMTER));
    }
    
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<Result> processMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
        logger.error("", ex);
        // logger.debug("request url:{}", request.getRequestURI());
        // logger.debug("request content:{}", JSON.toJSON(request.getParameterMap()));
        return ResponseEntity.ok().body(new Result(StatusCode.PARAMTER));
    }
    
    // @ExceptionHandler(NumberFormatException.class)
    // @ResponseStatus(HttpStatus.OK)
    // public ResponseEntity<Result> processNumberFormatException(HttpRequestMethodNotSupportedException exception) {
    // return ResponseEntity.ok().body(new Result(StatusCode.PARAMTER, exception.getMessage()));
    // }
    
}
